#! /bin/sh
# set PostScript Previewer here if environment variable PSPREVIEWER not set
VIEWER=$PSPREVIEWER

GRAPHER=psgraph
IMAGER=supsimage
WIGGER=supswigp
LINES=18
COMMANDS=Commands_Only

indata=data.seis
outdata1=data.fullfold
outdata2=data.sortdown
outdata3=data.unsort
outdata4=data.recgather
outdata5=data.stack
outdata6=data.near
outdata7=data.far

# Eventually put Model here
# First make the synthetic data for the sorting demo.
echo "Making synthetic data for demo"
######START OF MODEL######
REF1="1.0:-2,0.2;4,0.2"
REF2="1.0:-2,2.0199;3.5495,0"
REF3="1.0:-2,4.3955;3.2384,0"
REF4="1.0:-2,8.8603;3.1155,0"

dvdz=.015  # velocity gradient (dv/dz)
dvdx=-0.2  # velocity gradient (dv/dx)
v00=5.0  # P-wave velocity at surface in km/s
nxo=6 fxo=0.04 dxo=0.04 # offset information: number, first and spacing
nxs=12 fxs=1.0 dxs=0.04 # shot information: number, first and spacing
nt=200 dt=0.004         # time information: number and spacing

susynlv v00=$v00 ndpfz=10 dvdz=$dvdz dvdx=$dvdx \
		dt=$dt nt=$nt verbose=0 \
		ref=$REF1 ref=$REF2 ref=$REF3 ref=$REF4 \
		nxo=$nxo fxo=$fxo dxo=$dxo \
		nxs=$nxs fxs=$fxs dxs=$dxs  |
suchw key1=cdp key2=gx key3=sx b=1 c=1 d=2 >$indata
######END OF MODEL######
echo
echo "ADVICE: GO THROUGH THIS DEMO SLOWLY, THINKING CAREFULLY ABOUT EACH SCREENFUL"
echo
echo "Let's display the model data: 12 shot gathers each with 6 offsets"
$IMAGER <$indata perc=99 title="Shot Gathers"\
	label1="Time (sec)" label2="Shot Gather" f2=1 d2=0.166667 | $VIEWER
 
# SORTING EXAMPLES
# Use surange to get overview of header values set, etc.
echo
echo "########EXAMPLE: GET OVERVIEW OF DATA SET########"
echo "Use surange to see the header values that are set."
echo
echo "surange <$indata"
pause run command
surange <$indata
echo
echo "The results from surange are consistent with our model having"
echo "12 shot gathers of 6 traces each.  Indeed, we see that there are 72"
echo "traces, 12 shot points, offsets ranging from 40 to 240. Moreover,"
echo "each trace has 200 samples at 4ms sampling interval, etc."
echo
echo "Remarks: in this model, we've set the cdp to (sx + gx)/2, instead"
echo "of using the conventional numbering 1, 2, 3, ...  Also, to make things"
echo "interesting, the model has a whopping lateral velocity variation."

# Get a stacking chart
echo
echo
echo "Now get a stacking chart (the command is from the suchart self-doc):"
echo
echo "suchart <$indata |"            
echo "$GRAPHER n=72 title=\"Stacking Chart\" label1=\"sx\" label2=\"gx\" \\ "
echo "linewidth=0 marksize=2 mark=8 | $VIEWER"
pause run commands
suchart <$indata |                     
$GRAPHER n=72 title="Stacking Chart" label1="sx" label2="gx" \
 linewidth=0 marksize=2 mark=8 | $VIEWER                     
echo
echo "The stacking chart is consistent with the model having"
echo "its shot spacing equal to its geophone spacing.  Indeed, in such a"
echo "data set, the fold is always half the number of offsets, here 6/2 = 3,"
echo "and the offset values alternate in successive traces."

# Find region of full fold
echo
echo "########CONTINUE OVERVIEW AND DETERMINE REGION OF FULL FOLD########"
echo "We can pin these facts down precisely by using susort:"
echo "We sort to cdp on the fly to get region of full fold."
echo "Since the default sort key is \"cdp\", we need not mention it."
echo "After sorting, pipe into sugethw to verify the sort:"
echo
echo "susort <$indata | sugethw cdp offset"
echo
echo "Note: This list will over-run the screen, we hope you can scroll back."
echo "We promise not to do it again!"
pause run commands
susort <$indata | sugethw cdp offset
echo
echo "We now see that the region of full fold (here only 3-fold) is between"
echo "cdps 1100 and 1480."
echo
echo "Also note that the near offset alternates between 40 and 80,"
echo "while the far offset alternates between 200 and 240."
pause

# Window to full fold region and sort by cdp and offset
echo
echo "IMPORTANT:"
echo "We displayed the offset values along with the cdp values"
echo "to make a point:  On some machines, the \"natural\" offset"
echo "order is preserved; but on many machines it is not.  Even if your"
echo "your present compiler preserves the natural order, DON'T come to"
echo "depend on this---if you CARE about the order of the offsets, then"
echo "use SECONDARY SORT KEYS in susort applications.  We'll do this"
echo "in our next example."
echo
echo "########WINDOW TO REGION OF FULL FOLD########"
echo "Window to full fold region and sort by cdp and then by offsets"
echo "within a given cdp.  Afterwards check the cdp and offset order"
echo "(from now on we only show the first ${LINES} lines from sugethw):"
echo
echo "suwind <$indata key=cdp min=1100 max=1480 |"
echo "susort cdp offset >$outdata1"
echo "sugethw<$outdata1 cdp offset | sed ${LINES}q"
pause run commands
suwind <$indata key=cdp min=1100 max=1480 |
susort cdp offset >$outdata1
sugethw<$outdata1 cdp offset | sed ${LINES}q
echo "        . . ."
echo
echo
echo "Looks OK!"
echo
echo "And we see the expected two step alternation of the offsets."
echo
pause Display the selected data in cdp order
$IMAGER <$outdata1 perc=99 title="CDP and +Offset"\
	label1="Time (sec)" label2="CDP Gather" f2=1 d2=0.333333 | $VIEWER

# sort data by decreasing cdp and within each cdp by offset
echo "########EXAMPLE: SORT WITH DECREASING OFFSET ORDER########"
echo "Now we exercise the sort options without regard to geophysical"
echo "meaning.  First, re-sort the full-fold data by cdp, but within each cdp"
echo "use DECREASING offset and check result with sugethw."
echo
echo "susort <$outdata1 cdp -offset >$outdata2"
echo "sugethw <$outdata2 cdp offset | sed ${LINES}q"
pause run command
susort <$outdata1 cdp -offset >$outdata2 
sugethw <$outdata2 cdp offset | sed ${LINES}q
echo "        . . ."
echo
pause Display the full fold data in reverse offset order
$IMAGER <$outdata2 perc=99\
	title="CDP and -Offset"\
 	label1="Time (sec)" label2="CDP Gather" f2=1 d2=0.333333 | $VIEWER

# reconstruct windowed data $outdata1 from $outdata2
echo
echo "########EXAMPLE: RE-SORT TO INCREASING OFFSET ORDER AND CHECK########"
echo "Next, return to increasing cdp order and check:"
echo
echo "susort <$outdata2 cdp offset >$outdata3"
echo "sudiff $outdata1 $outdata3 | sumax"
pause run commands
susort <$outdata2 cdp offset >$outdata3
sudiff $outdata1 $outdata3 | sumax
echo
echo "Good, no difference in the two data sets."

# make receiver gathers from shot gathers
echo
echo "########EXAMPLE: GET RECEIVER SORT########"
echo "Next, make receiver gathers from original shot gathers:"
echo
echo "susort <$indata gx offset >$outdata4"
echo "sugethw <$outdata4 gx sx cdp offset | sed ${LINES}q"
pause run commands
susort <$indata gx offset >$outdata4
sugethw <$outdata4 gx sx cdp offset | sed ${LINES}q
echo "        . . ."
echo
pause Display receiver gathers--receiver numbering is arbitrary
$IMAGER <$outdata4 perc=99 title="Receiver Gathers"\
	label1="Time (sec)" label2="Receiver Gather" f2=1 d2=1 | $VIEWER

# application: stacking
echo
echo "########EXAMPLE: STACK THE DATA########"
echo "We sort the full fold data by cdp and then stack it."
echo "Now we don't care about offset ordering and can just"
echo "use the default susort and sustack keys which are both \"cdp\"."
echo "After stacking, use sugethw to check the fold values (nhs keyword)."
echo
echo "susort <$outdata1 |"
echo "sustack >$outdata5"
echo "sugethw <$outdata5 cdp nhs | sed ${LINES}q"
pause run commands
susort <$outdata1 |
sustack >$outdata5
sugethw <$outdata5 cdp nhs | sed ${LINES}q
echo "        . . ."
echo
pause Display the stacked data
$WIGGER <$outdata5 perc=99 title="Stacked Data"\
	label1="Time (sec)" label2="CDP Location" f2=1100 d2=20 | $VIEWER

# get near and far common offset sections (doesn't use susort, but ...)
echo
echo "########EXAMPLE: GET NEAR OFFSET SECTION########"
echo "Get near and far common offset sections (doesn't use susort, but ...)"
echo "Recall that the near offsets in the full-fold portion of the data"
echo "vary between 0 and 40 and the fars between 160 and 200."
echo "First the nears:"
echo
echo "suwind <$indata key=offset min=40 max=80 >$outdata6"
echo "sugethw <$outdata6 cdp offset | sed ${LINES}q"
pause run command
suwind <$indata key=offset min=40 max=80 >$outdata6
sugethw <$outdata6 cdp offset | sed ${LINES}q
echo "        . . ."
echo
pause Display the near offset data
$WIGGER <$outdata6 perc=99 title="Near Offsets"\
	label1="Time (sec)" label2="CDP Location" f2=1020 d2=20 | $VIEWER

echo
echo "########EXAMPLE: GET FAR OFFSET SECTION########"
echo "Then the fars:"
echo "suwind <$indata key=offset min=200 max=240 >$outdata7"
echo "sugethw <$outdata7 cdp offset | sed ${LINES}q"
pause run commands
suwind <$indata key=offset min=200 max=240 >$outdata7
sugethw <$outdata7 cdp offset | sed ${LINES}q
echo "        . . ."
echo
pause Display the far offset data
$WIGGER <$outdata7 perc=99 title="Far Offsets"\
	label1="Time (sec)" label2="CDP Location" f2=1100 d2=20 | $VIEWER
echo
echo "Observe the two step \"jitter\" in the shallow layer.  This is typical"
echo "of data gathered with shot spacing equal to receiver spacing."
echo
echo "For your convenience, the file $COMMANDS contains the commands"
echo "used in this demo, with brief comments, but without the lines serving"
echo "to control the flow of the demo."
echo "########EXITING SORT DEMO########"
exit
